#include "../main.h"

#define MAX_EVENTS 10
int main()
{
    int epfd=epoll_create(1);
    if(epfd==-1){
        perror("epoll_create:");
        exit(EXIT_FAILURE);
    }
    struct epoll_event event={.events=EPOLLIN,.data.fd=0};
    int ret=epoll_ctl(epfd,EPOLL_CTL_ADD,0,&event);
    if(ret==-1){
        perror("epoll_ctl");
        close(epfd);
        exit(EXIT_FAILURE);
    }
    struct epoll_event events[MAX_EVENTS];
    while(1){
        int res=epoll_wait(epfd,events,MAX_EVENTS,-1);
        if(res==-1){
            perror("epoll_wait:");
            close(epfd);
            exit(EXIT_FAILURE);
        }else if(res==0){
            printf("timeout\n");
        }else{
            for(int i=0;i<MAX_EVENTS;i++){
                if(events[i].data.fd==0){
                    char buf[128]={0};
                    fgets(buf,sizeof buf,stdin);
                    fputs(buf,stdout);
                    if(strncmp(buf,"exit",4)==0){
                        close(epfd);
                        exit(EXIT_SUCCESS);
                    }
                }
            }
        }
    }
    close(epfd);
    return 0;
}

